package com.example.lexiangbackend.repository;

import com.example.lexiangbackend.entity.Favorite;
import com.example.lexiangbackend.entity.Music;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.Optional;

@Repository
public interface FavoriteRepository extends JpaRepository<Favorite, Long> {
    
    /**
     * 根据用户ID查找收藏列表
     */
    Page<Favorite> findByUserUserId(Long userId, Pageable pageable);
    
    /**
     * 检查用户是否收藏了某首歌
     */
    boolean existsByUserUserIdAndMusicMusicId(Long userId, Long musicId);
    
    /**
     * 根据用户ID和音乐ID查找收藏记录
     */
    Optional<Favorite> findByUserUserIdAndMusicMusicId(Long userId, Long musicId);
    
    /**
     * 获取用户收藏的音乐列表
     */
    @Query("SELECT f.music FROM Favorite f WHERE f.user.userId = :userId ORDER BY f.favoriteTime DESC")
    Page<Music> findMusicByUserId(@Param("userId") Long userId, Pageable pageable);
    
    /**
     * 统计用户收藏数量
     */
    long countByUserUserId(Long userId);
    
    /**
     * 统计音乐被收藏次数
     */
    long countByMusicMusicId(Long musicId);
}